package com.space.components.domain.core.diff;

/**
 * @author <a href="mailto:414563035@qq.com">Bedroom</a>
 * @date 2021/5/26 3:20 下午
 */
public class ItemDiff<T> {
    private final DiffType type;

    private final T newValue;

    private final T oldValue;

    private final T diffValue;

    public ItemDiff(DiffType type, T newValue, T oldValue, T diffValue) {
        this.type = type;
        this.newValue = newValue;
        this.oldValue = oldValue;
        this.diffValue = diffValue;
    }

    public ItemDiff(DiffType type, T newValue, T oldValue) {
        this(type, newValue, oldValue, null);
    }

    public T getValue() {
        if (type == DiffType.Removed) {
            return oldValue;
        } else if (type == DiffType.Modified) {
            return diffValue;
        } else {
            return newValue;
        }
    }

    public Object getOldValue() {
        return oldValue;
    }

    public Object getNewValue() {
        return newValue;
    }

    public DiffType getType() {
        return type;
    }

    @Override
    public String toString() {
        return "ItemDiff{" +
                "type=" + type +
                ", newValue=" + newValue +
                ", oldValue=" + oldValue +
                ", diffValue=" + diffValue +
                '}';
    }
}
